<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('上级标签')" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="tagsOptions"
              :show-count="true"
              :placeholder="$t('请选择')"
              :normalizer="normalizer"
              @select="parentIdSelect"
            >
              <label
                slot="option-label"
                slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }"
                :class="labelClassName"
              >
                {{ node.label }}
                <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
              </label>
            </treeselect>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('标签名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('标签名称')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('标签编码')" prop="encode">
            <el-input v-model="form.encode" :placeholder="$t('标签编码')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否可编辑')">
            <el-radio-group v-model="form.edit" :disabled="form.parentId !== '0' || form.hasChildren">
              <el-radio
                v-for="dict in editOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" :placeholder="$t('排序')" :min="1" :max="9999" :precision="0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:tags" />
    </div>
  </div>
</template>

<script>
import { saveDealTagsInfo, getDealTagsInfo, getSelectTree } from '@/api/deal/tags/dealTagsInfo'
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: {
    Treeselect
  },
  props: {
    configureId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        configureId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        encode: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        parentId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      // 是否编辑字典
      editOptions: [],
      // 标签tree data
      tagsOptions: []
    }
  },
  created() {
    this.getDicts('sys_yes_no').then(response => {
      this.editOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      this.loadTagsTree()
      if (data != null && data.id !== undefined) {
        getDealTagsInfo(data.id).then(response => {
          this.form = response.data
          this.form.hasChildren = data.hasChildren
          this.loading = false
        })
      } else {
        this.reset()
        if (data?.parentId) {
          this.form.parentId = data.parentId
        }
        this.loading = false
      }
    },
    loadTagsTree() {
      getSelectTree(this.configureId).then(res => {
        this.tagsOptions = []
        const tag = {
          id: '0',
          name: this.$t('根标签'),
          children: []
        }
        tag.children = res.data
        this.tagsOptions.push(tag)
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        configureId: this.configureId,

        parentId: '0',

        edit: 'N',

        name: undefined,

        encode: undefined,

        sort: undefined

      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDealTagsInfo(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    parentIdSelect(node) {
      if (node.id !== '0') {
        this.form.edit = 'N'
      }
    },
    normalizer(node) {
      return {
        label: node.name,
        isDisabled: node.edit === 'Y'
      }
    }
  }
}
</script>
